// pages/test-inbook/test-inbook.js
var util = require('../../utils/util.js')
const DB = wx.cloud.database()
const $ = DB.command.aggregate
const _ = DB.command
//获取应用实例
const app = getApp()

let skip = 20

Page({

  /**
   * 页面的初始数据
   */
  data: {
     //处理导 航 栏的高度
     rpxdata: wx.getStorageSync('rpxdata'),
     navigationBarAndStatusBarHeight: wx.getStorageSync('statusBarHeight') + wx.getStorageSync('navigationBarHeight') + 'px',
     navigationBarAndStatusBarAndsearchHeight: wx.getStorageSync('statusBarHeight') + wx.getStorageSync('navigationBarHeight') + 60 + 'px',
     //设置导航栏的显示信息
     navbarData: {
       showCapsule: 1,
       title: "库存图书"
     },
 
   DB_jy:{},
   bottom_info: 0

  },

  jieyuedate: function(startime) {
    //日期格式化
    var start_date = new Date(startime.replace(/-/g, "/"));
    var end_date = new Date();
    //转成毫秒数，两个日期相减
    var ms = end_date.getTime() - start_date.getTime();
    //转换成天数
    var day = parseInt(Math.ceil(ms / (1000 * 60 * 60 * 24)));
    //do something
    return day    
  } ,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that =this
    let dbtmp = []
    DB.collection("lchbook").orderBy('uptime','desc').where({jystate :false}).skip(0).limit(20).get({success:(res)=>{   
      for (let i=0;i<res.data.length;i++){ 
        var obj = {}
        obj.bookname = res.data[i].bookname
        obj.jyren = res.data[i].jyren
        obj.jydate = res.data[i].jydate
        obj.isbn = res.data[i].isbn
        obj.day = that.jieyuedate(res.data[i].jytime)
        obj._id = res.data[i]._id
        obj.jynum = res.data[i].jynum
        dbtmp.push(obj)
      }
      that.setData({DB_jy:dbtmp})      
    }  })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this
    let dbtmp = []
    let vvv = this.data.DB_jy
    DB.collection("lchbook").orderBy('uptime','desc').where({jystate :false}).skip(skip).limit(20).get({success:(res)=>{
      for (let i=0;i<res.data.length;i++){ 
        var obj = {}
        obj.bookname = res.data[i].bookname
        obj.jyren = res.data[i].jyren
        obj.jydate = res.data[i].jydate
        obj.isbn = res.data[i].isbn
        obj.day = that.jieyuedate(res.data[i].jytime)
        obj._id = res.data[i]._id
        obj.jynum = res.data[i].jynum
        dbtmp.push(obj)
      }
      vvv = that.data.DB_jy.concat(dbtmp)
      if (res.data.length == 0) {that.setData({bottom_info: 1 })}
      else { that.setData({DB_jy:vvv})   
      skip = skip + 20
    }
    }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})